package online.sixfish.judger.judger;


import lombok.extern.slf4j.Slf4j;
import online.sixfish.judger.pojo.TestPack;
import online.sixfish.judger.pojo.TestResult;
import org.apache.commons.lang3.StringUtils;

/**
 * @author 凯威
 * 判题辅助,集成判题机核心功能
 */
@Slf4j
public class Judger extends JudgeCore {


    public Judger(TestPack testPack) {
        this.testPack = testPack;
        log.info("testPack:{}", testPack.toString());
    }

    /**
     * 完整的判题机运行过程
     */
    public TestResult run() {
        long start = System.currentTimeMillis();
        //初始化
        this.init();
        String compile = this.compile();
        log.info("编译结果：{}", compile);
        // 编译结果需要为空或null才是编译成功，可以继续执行代码
        if (StringUtils.isNotBlank(compile)) {
            log.info("编译错误");
            testResult.setPass(false);
            testResult.setTitle("Compile Error");
            testResult.setMessage(compile);
            long end = System.currentTimeMillis();
            log.info("耗时：{}ms", end - start);
            return testResult;
        }

        log.info("编译成功");
        String execute = this.execute();
        log.info("执行结果：{}",execute);

        checkAnswer();
        if (testResult.isPass()) {
            log.info("题目测试pass");
        }
        long end = System.currentTimeMillis();
        log.info("耗时：{}ms", end - start);
        return testResult;

    }
}
